package com.meituan.catering.management.order.biz.validator;

import com.meituan.catering.management.order.api.http.model.enumeration.CateringOrderItemAccessoryStatusEnum;
import com.meituan.catering.management.order.api.http.model.enumeration.CateringOrderItemStatusEnum;
import com.meituan.catering.management.order.dao.mapper.CateringOrderItemAccessoryMapper;
import com.meituan.catering.management.order.dao.mapper.CateringOrderItemMapper;
import com.meituan.catering.management.order.dao.mapper.CateringOrderMapper;
import com.meituan.catering.management.order.dao.model.CateringOrderItemAccessoryDO;
import com.meituan.catering.management.order.dao.model.CateringOrderItemDO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author Believe
 * Date 2022/6/2 15:40
 */
@Component
public class OrderBizServiceValidator {

    @Resource
    private CateringOrderMapper cateringOrderMapper;

    @Resource
    private CateringOrderItemMapper cateringOrderItemMapper;

    @Resource
    private CateringOrderItemAccessoryMapper cateringOrderItemAccessoryMapper;

    public Boolean statusValid(Long tenantId, Long orderId) {
        List<CateringOrderItemDO> cateringOrderItemDOList = cateringOrderItemMapper.findById(tenantId, orderId);
        Set<Long> validProductIds = cateringOrderItemDOList.stream().map(CateringOrderItemDO::getId).collect(Collectors.toSet());
        List<CateringOrderItemAccessoryDO> accessoryDOList = cateringOrderItemAccessoryMapper.findById(tenantId, validProductIds);
        boolean orderItemFlag = cateringOrderItemDOList.stream().anyMatch(item -> !item.getStatus().equals(CateringOrderItemStatusEnum.PREPARED));
        boolean accessoryFlag = accessoryDOList.stream().anyMatch(item -> !item.getStatus().equals(CateringOrderItemAccessoryStatusEnum.PREPARED));
        if (orderItemFlag || accessoryFlag) {
            return false;
        }
        return true;
    }
}
